package com.zkk.web;

import com.alibaba.fastjson.JSON;
import com.zkk.pojo.Card;
import com.zkk.pojo.DailyRecord;
import com.zkk.pojo.TransferAccount;
import com.zkk.service.CardService;
import com.zkk.service.DailyRecordService;
import com.zkk.service.JdbcCardService;
import com.zkk.service.TransferAccountService;
import com.zkk.service.impl.CardServiceImpl;
import com.zkk.service.impl.DailyRecordServiceImpl;
import com.zkk.service.impl.JdbcCardServiceImpl;
import com.zkk.service.impl.TransferAccountServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;


import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

@WebServlet("/transferMoney")
public class transfer_accountServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //得到其他表的账户
        String name = req.getParameter("name");
        String card_no = req.getParameter("card_no");
        String bank = req.getParameter("bank");
        //得到前端验证码
        String code = req.getParameter("code");
        Object attribute = req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //得到转账银行卡号
        String id_card = req.getParameter("id_card");
        String money = req.getParameter("money");
        //得到支付密码
        String payment_pwd = req.getParameter("payment_pwd");
        CardService cardService = new CardServiceImpl();
        Card card = new Card();
        card.setId_card(id_card);
        //把前端得到的转账金额字符串转换数据类型
        BigDecimal transferMoney = new BigDecimal(money);

        TransferAccountService transferAccountService = new TransferAccountServiceImpl();
        //设置值到实体类
        TransferAccount transferAccount = new TransferAccount();
        transferAccount.setName(name);
        transferAccount.setBank(bank);
        transferAccount.setCard_no(card_no);
        //创建转账业务层对象
        JdbcCardService jdbcCardService = new JdbcCardServiceImpl();
        if(name!=null & code.equals(attribute) & payment_pwd.equals("123456")){
            if(transferAccountService.queryByCard_no(card_no)==null){
                //如果查询到的账号等于空，就先去添加这个账号
                int account = transferAccountService.add(transferAccount);
                int i = jdbcCardService.onInputMoney(id_card, card_no, transferMoney);
                if(i==1){
                    DailyRecordService dailyRecordService = new DailyRecordServiceImpl();
                    DailyRecord dailyRecord = new DailyRecord();
                    dailyRecord.setOutPutCard_Id(id_card);
                    dailyRecord.setInPutCard_Id(card_no);
                    Date date = new Date();
                    dailyRecord.setDate(date);
                    dailyRecord.setMoney(transferMoney);
                    dailyRecordService.record(dailyRecord);
                }

                String s = JSON.toJSONString(0);
                resp.getWriter().write(s);
            }else {
                //如果账号已经存在，先查询到这个账户余额，获取余额
           /* TransferAccount transferAccount1 = transferAccountService.queryByCard_no(card_no);
            BigDecimal property = transferAccount1.getProperty();
            //把查询到的余额和转账的钱相加，更新到账户中去
            BigDecimal add = property.add(transferMoney);*/
                int i = jdbcCardService.onInputMoney(id_card, card_no, transferMoney);
                if(i==1){
                    DailyRecordService dailyRecordService = new DailyRecordServiceImpl();
                    DailyRecord dailyRecord = new DailyRecord();
                    dailyRecord.setOutPutCard_Id(id_card);
                    dailyRecord.setInPutCard_Id(card_no);
                    Date date = new Date();
                    dailyRecord.setDate(date);
                    dailyRecord.setAction("转账");
                    dailyRecord.setMoney(transferMoney);
                    dailyRecordService.record(dailyRecord);

                }



                String s = JSON.toJSONString(1);
                resp.getWriter().write(s);
            }

        }else {
            String s = JSON.toJSONString(2);
            resp.getWriter().write(s);
        }



    }
}
